﻿using System;
using HenIT.Text;
using System.Data;

namespace TestHTMLWriter
{
    public class Program
    {
        private static bool writeToFile = false;
        static void Main(string[] args)
        {
            writeToFile = true;

            Console.WriteLine("General test");
            TestGeneralStuff();
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();

            Console.WriteLine("Auto formatting test");
            TestAutoFormattingStuff();
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();

            Console.WriteLine("Lists test");
            TestListStuff();
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();

            Console.WriteLine("Manual table test");
            TestManualTable();
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();

            Console.WriteLine("Data table test");
            TestDataTable();
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();

            Console.WriteLine("Iframe test");
            TestIframeStuff();
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();

            Console.WriteLine("Bargraph test");
            TestBarGraph();
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();

            Console.WriteLine();

            Console.WriteLine("A couple random escaped HTML special characters");
            Console.WriteLine("Less than - {0}", HTMLWriter.GetEscapedSpecialCharacter(HTMLSpecialCharacter.LessThanSign));
            Console.WriteLine("Greater than - {0}", HTMLWriter.GetEscapedSpecialCharacter(HTMLSpecialCharacter.GreaterThanSign));
            Console.WriteLine("Divide - {0}", HTMLWriter.GetEscapedSpecialCharacter(HTMLSpecialSymbolCharacter.Divide));
            Console.WriteLine("Yacute - {0}", HTMLWriter.GetEscapedSpecialCharacter(HTMLSpecialLetterCharacter.UYacute));
            Console.WriteLine("Ntilde - {0}", HTMLWriter.GetEscapedSpecialCharacter(HTMLSpecialLetterCharacter.UNtilde));
            Console.WriteLine("Copy - {0}", HTMLWriter.GetEscapedSpecialCharacter(HTMLSpecialSymbolCharacter.Copy));

            string testString = "ABBA was a Swedish pop group formed in Stockholm in 1972, comprising Agnetha Fältskog, Benny Andersson, Björn Ulvaeus and Anni-Frid Lyngstad";
            Console.WriteLine("Unescaped text");
            Console.WriteLine(testString);
            if (HTMLWriter.ContainsAnySpecialCharacters(testString))
                Console.WriteLine("Escaped text\r\n" + HTMLWriter.EscapeText(testString));

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        private static void TestGeneralStuff()
        {
            HTMLWriter h = new HTMLWriter("General test");
            h.AppendStyle("body", "font-family:Arial;")
                .AppendStyle("abbr", "color:blue; font-style:italic;");
            h.AppendExternalStylesheet("somesheet.css");
            h.AppendHeading(1, "Heading 1")
                .AppendHeading(2, "Heading 2")
                .AppendHeading(4, "Heading 4")
                .AppendParagraphStart().AppendText("After the ").AppendItalicText("headings").AppendTagEnd()
                .AppendHorisontalLine()
                .AppendParagraph("After hr");
            h.AppendNewLine();
            h.AppendBlockquoteStart();            
            h.AppendParagraph("Hello lion", "stuff");
            h.AppendParagraphStart().AppendText("Some more text.")
                .AppendLineBreak()
                .AppendAnchor("example.htm", "Back to myself");
            h.AppendAllEndTagsUntil("blockquote");
            h.AppendHorisontalLine().AppendHorisontalLine().AppendHorisontalLine()
                .AppendText("Just because I can");
            h.AppendBlockquoteStart()
                .AppendCode("Just because I can<br />More I cans")
                .AppendLineBreak()
                .AppendText("The following is an abbreviation: ")
                .AppendAddressStart()
                .AppendAbbreviation("http")
                .AppendText("://nowhere.in.particular")
                .AppendTagEnd(2);

            if (writeToFile)
                h.WriteToFile(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "generaltest.htm"));
            Console.WriteLine(h.ToString());
        }

        private static void TestAutoFormattingStuff()
        {
            HTMLWriter h = new HTMLWriter("Auto formatting test", true);
            h.AppendStyle("body", "font-family:Arial;");
            h.AppendExternalStylesheet("somesheet.css");
            h.AppendHeading(1, "Example")
                .AppendParagraphStart().AppendText("After ").AppendItalicText("heading1").AppendTagEnd()
                .AppendHorisontalLine()
                .AppendParagraph("After hr");
            h.AppendNewLine();
            h.AppendHeading(1, "Line sample")
                .AppendHorisontalLine();
            h.AppendBlockquoteStart();
            h.AppendParagraph("Hello lion", "stuff");
            h.AppendParagraphStart().AppendText("Some more text.")
                .AppendLineBreak()
                .AppendAnchor("example.htm", "Back to myself")
                .AppendTagEnd(); //par
            h.AppendTagEnd();//blockquote

            h.AppendDivStart()
                .AppendText("Stuff")
                .AppendTagEnd();
            h.AppendDivStart()
                .AppendText("Stuff2")
                .AppendTagEnd();
            h.AppendDivStart()
                .AppendText("Stuff3")
                .AppendTagEnd();
            h.AppendHorisontalLine().AppendHorisontalLine().AppendHorisontalLine()
                .AppendText("Just because I can");
            h.AppendBlockquoteStart()
                .AppendCode("Just because I can<br />More I cans")
                .AppendLineBreak()
                .AppendText("The following is an abbreviation: ")
                .AppendAddressStart()
                .AppendAbbreviation("http")
                .AppendText("://nowhere.in.particular")
                .AppendTagEnd()
                .AppendTagEnd();
            if (writeToFile)
                h.WriteToFile(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Autoformattingtest.htm"));
            Console.WriteLine(h.ToString());
        }

        private static void TestListStuff()
        {
            HTMLWriter h = new HTMLWriter("List test");
            h.AutoFormatting = true;
            h.AutoIndentation = true;
            h.AppendStyle("body", "font-family:Arial;");
            h.AppendBlockquoteStart();
            h.AppendUnOrderedListStart("mylist")
                .AppendListItem("Item 1")
                .AppendListItem("Item 2")
                .AppendListItem("Item 3")
                .AppendTagEnd();
            h.AppendDefinitionListStart()
                .AppendDefinitionTerm("Me")
                .AppendDefinitionDescription("is cool")
                .AppendTagEnd("dl"); //showing autoPop
            h.AppendTagStart("tt","test")
                .AppendParagraph("Hello")
                .AppendParagraphStart()
                .AppendText("this is a quote: ")
                .AppendQuote("the quote")
                .AppendTagEnd() //paragraph
                .AppendTagEnd(); //tt
            h.AppendTagEnd();//blockquote
            if (writeToFile)
                h.WriteToFile(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "liststest.htm"));
            Console.WriteLine(h.ToString());
        }
        
        private static void TestManualTable()
        {
            HTMLWriter h = new HTMLWriter("Table test");
            h.AutoFormatting = true;
            h.AutoIndentation = true;
            h.AppendStyle("body", "font-family:Arial;")
                .AppendStyle(".blue", "background-color:lightblue;")
                .AppendStyle(".red", "background-color:#CAA;")
                .AppendStyle(".green", "background-color:lightgreen;")
                .AppendStyle(".fullWidth", "width:100%;")
                .AppendStyle(".alignleft", "text-align:left;")
                .AppendStyle("thead", "display: table-header-group;")
                .AppendStyle("tfoot", "display: table-footer-group;");

            h.AppendHeading(1, "Manual table test");
            h.AppendBlockquoteStart();

            //table example
            h.AppendTableStart("main", new CustomAttribute("border", "0"))
                .AppendTableHeadStart()
                .AppendTableRowStart()
                .AppendTableCellStart("", new CustomAttribute("colspan", "2"))
                .AppendText("Header")
                .AppendTagEnd() //cell
                .AppendTagEnd() //row
                .AppendTagEnd() //thead
                .AppendTableFootStart()
                .AppendTableRowStart()
                .AppendTableCell("Footer", "", new CustomAttribute("colspan", "2"))
                .AppendTagEnd() //row
                .AppendTagEnd() //tfoot
                .AppendTableRowStart()
                .AppendTableHeaderCellStart()
                .AppendText("Name")
                .AppendTagEnd() //cell
                .AppendTableHeaderCellStart()
                .AppendText("Surname")
                .AppendTagEnd()
                .AppendTagEnd()
                .AppendTableRowStart()
                .AppendTableCellStart("", new CustomAttribute("colspan", "2"))
                .AppendText("test2")
                .AppendTagEnd()
                .AppendTagEnd() //row
                .AppendTagEnd(); //table
            
            h.AppendTagEnd();//blockquote

            h.AppendBlockquoteStart()
                .AppendTableStart()
                .AppendTableRowStart()
                .AppendTableCell("Test")
                .AppendTagEnd() //row
                .AppendTagEnd() //table
                .AppendTagEnd(); //blockquote
            h.AppendBlockquoteStart()
                .AppendParagraph("Before table")
                .AppendTableStart()
                .AppendTableRowStart()
                .AppendTableCell("Test")
                .AppendTagEnd() //row
                .AppendTagEnd() //table
                .AppendParagraph("After table")
                .AppendTagEnd(); //blockquote
            if (writeToFile)
                h.WriteToFile(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "manualtabletest.htm"));
            Console.WriteLine(h.ToString());
        }

        private static void TestDataTable()
        {
            HTMLWriter h = new HTMLWriter("Data table test");
            h.AutoFormatting = true;
            h.AutoIndentation = true;
            h.AppendStyle("body", "font-family:Arial;")
                .AppendStyle(".blue", "background-color:lightblue;")
                .AppendStyle(".red", "background-color:#DCC;")
                .AppendStyle(".green", "background-color:white;")
                .AppendStyle(".fullWidth", "width:100%;")
                .AppendStyle(".alignleft", "text-align:left;")
                .AppendStyle("thead", "display: table-header-group;")
                .AppendStyle("tfoot", "display: table-footer-group;");

            DataTableFormatSettings alternatingRowSettings = new DataTableFormatSettings();
            alternatingRowSettings.RowClassNames = new string[] { "blue", "red", "white" };
            alternatingRowSettings.LeftAlignHeadings = false;
            alternatingRowSettings.ColumnHeaderClass = "alignleft";
            alternatingRowSettings.LinkIDField = "Id";
            alternatingRowSettings.LinkDisplayField = "Name";
            alternatingRowSettings.LinkPageUrl = "edit";
            DataTable table = new DataTable("Test");
            table.Columns.AddRange(new DataColumn[] 
            { 
                new DataColumn("Id", typeof(int)), 
                new DataColumn("Name", typeof(string)), 
                new DataColumn("Surname", typeof(string)) 
            });
            table.Rows.Add(new object[] { "1", "Piet", "Pompies" });
            table.Rows.Add(new object[] { "2", "Koos", "Pompies" });
            table.Rows.Add(new object[] { "3", "Jan", "Pompies" });
            table.Rows.Add(new object[] { "4", "Piet", "van der Merwe" });
            table.Rows.Add(new object[] { "5", "Koos", "van der Merwe" });
            table.Rows.Add(new object[] { "6", "Jan", "van der Merwe" });
            for (int i = 7; i < 60; i++)
            {
                table.Rows.Add(new object[] { i.ToString(),  "Guy " + i.ToString(), "Nukem" });
            }
            h.AppendBlockquoteStart()
                .AppendDataTable(table, alternatingRowSettings, "fullWidth")
                .AppendTagEnd() //blockquote
                .AppendHorisontalLine();
            if (writeToFile)
                h.WriteToFile(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "datatabletest.htm"));
            Console.WriteLine(h.ToString());
        }

        private static void TestIframeStuff()
        {
            HTMLWriter h = new HTMLWriter("General test");
            h.AutoFormatting = true;
            h.AutoIndentation = true;
            h.AppendStyle("body", "font-family:Arial;");
            h.AppendHeading(1, "Parent Example");
            h.AppendIframe("childpage.htm", "hello", "100%", "400px", true);
            if (writeToFile)
                h.WriteToFile(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "parenttest.htm"));
            Console.WriteLine(h.ToString());

            HTMLWriter h2 = new HTMLWriter("Child page");
            h2.AutoFormatting = true;
            h2.AutoIndentation = true;
            h2.AppendStyle("body", "font-family:Arial;");
            h2.AppendHeading(1, "Child Example");
            if (writeToFile)
                h2.WriteToFile(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "childpage.htm"));
            Console.WriteLine(h2.ToString());
        }

        private static void TestBarGraph()
        {
            HTMLWriter h = new HTMLWriter("Bar graph test");
            h.AutoFormatting = true;
            h.AutoIndentation = true;
            h.AppendStyle("body", "font-family:Arial;")
                .AppendStyle(".textLeft", "text-align:left;")
                .AppendStyle(".textRight", "text-align:right;")
                .AppendStyle(".aThird", "width:33%;")
                .AppendStyle(".fullwidth", "width:100%;")
                .AppendStyle(".bar", "height:20px;")
                .AppendStyle(".leading", "background-color:white;")
                .AppendStyle(".perc", "background-color: lightblue;")
                .AppendStyle(".remainder", "background-color:white;")
                .AppendStyle("thead", "display: table-header-group;")
                .AppendStyle("tfoot", "display: table-footer-group;");

            h.AppendHeading(1, "Example bar graph");

            h.AppendTableStart("fullwidth", new CustomAttribute("border", "0"))
                .AppendTableHeadStart()
                .AppendTableRowStart()
                .AppendTableHeaderCell("Name", "textLeft", new CustomAttribute("width", "15%")) //cell
                .AppendTableHeaderCell("Age", "textLeft", new CustomAttribute("width", "5%")) //cell
                .AppendTableCellStart()
                    .AppendTableStart("fullwidth", new CustomAttribute("border", "0"))
                    .AppendTableRowStart()
                    .AppendTableHeaderCell("0", "aThird textLeft")
                    .AppendTableHeaderCell("50", "aThird")
                    .AppendTableHeaderCell("100", "aThird textRight")
                    .AppendTagEnd() //row
                    .AppendTagEnd() //table
                .AppendTagEnd() //cell
                .AppendTagEnd() //row
                .AppendTagEnd() //thead
                .AppendTableBodyStart();
            Random r = new Random(DateTime.Now.Millisecond);
            for (int i = 1; i < 100; i++)
            {
                int leadingspace = r.Next(0, 50);
                int percentage = r.Next(leadingspace, 100 - leadingspace);
                int remainder = 100 - percentage - leadingspace;
                
                h.AppendTableRowStart();
                h.AppendTableCell("Item " + i.ToString());
                h.AppendTableCell(leadingspace.ToString("d2") + "-" + (leadingspace + percentage).ToString("d2"));
                h.AppendTableCellStart()
                    .AppendTableStart("fullwidth")
                    .AppendTableRowStart("bar")
                    .AppendTableCell(" ", "leading", new CustomAttribute("width", leadingspace.ToString() + "%"))
                    .AppendTableCell(" ", "perc", new CustomAttribute("width", percentage.ToString() + "%"))
                    .AppendTableCell(" ", "remainder", new CustomAttribute("width", remainder.ToString() + "%"))
                    .AppendTagEnd(4);
            }

            h.AppendTagEnd() //tbody
                .AppendTagEnd(); //table
            h.AppendImage("NoImage.jpg", "No image");

            if (writeToFile)
                h.WriteToFile(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "BarGraphtest.htm"));
            Console.WriteLine(h.ToString());
        }
    }
}
